package com.group.controller;

import com.group.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.entity.Record;
import pojo.entity.User;
import utils.BaseController;
import utils.BrowserUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "用户登录")
@Controller
@RequestMapping(value = "/login" , produces = "text/html;charset=UTF-8")
public class LoginController extends BaseController {

    @Autowired
    private LoginService loginService;
    BrowserUtils browserUtils=new BrowserUtils();

    @ApiOperation("QQ邮箱验证")
    @RequestMapping(value = "/email.do")
    @ResponseBody
    public String sendEmail(String userEmail, HttpServletResponse response){
        String che = loginService.sendEmail(userEmail, response);
        String json = writerSuccessResult(che, response);
        System.out.println("**********************************************"+json);
        return json;
    }

    @ApiOperation("用户注册")
    @RequestMapping(value = "/register.do")
    @ResponseBody
    public String Register(String userName, String password, String email, Integer type, HttpServletRequest request){
        String url = request.getHeader("USER-AGENT");
        String  browser = browserUtils.getBrowser(url);
//        System.out.println(type);

        String md5 = new SimpleHash("MD5", password, null, 3).toHex();

        User user=new User(userName,md5,email,100,10000,type);
        System.out.println(user);
        loginService.saveUser(user);
        Record record=new Record(user.getUserID(),0,0,2,browser);
        loginService.saveLog(record);
        String json = writerSuccessResult(user);
        return json;
    }

}
